import { defineStore } from 'pinia'

const routerConfig = defineStore('routerConfig', {
    state: () => {
        return {
            //面包屑
            breadcrumbs: [],
            //tag
            tagViews: [],
            //当前tagIndex
            tagIndex: -1,
            //是否在主页
            isOnlyHome: true
        }
    },

    actions: {
        //处理面包屑数据
        breadcrumbsHandle(currentRoute) {
            this.breadcrumbs = currentRoute.matched.filter(it => JSON.stringify(it.meta) !== '{}')
            this.isOnlyHome = !!this.breadcrumbs.filter(it => it.path === '/home').length
        },

        //处理tagView数据
        tagViewsHandle(currentRoute, type) {
            if (type === 'add') {
                this.tagViews.push(currentRoute)
                this.tagIndex = this.tagViews.length - 1
                return
            }
        },

        //直接给tagViews赋值
        cleanTagViews() {
            this.tagViews = []
        },

        //删除tagView
        delTagViewsHandle(idx) {
            this.tagViews.splice(idx, 1)
        },

        //根据参数关闭对应面包屑
        delMapTagHandle(path) {
            let idx = this.tagViews.findIndex(it => it.path == path)
            if (idx != -1) {
                this.delTagViewsHandle(idx)
            }
        },

        //处理当前激活下标
        tagIndexHandle(idx) {
            this.tagIndex = idx
        }
    }
})

export { routerConfig }